package com.future.controller;


import com.future.AccessToken.PrividerToken;
import com.future.Privider.AccessPrivider;
import com.future.pojo.Users;
import com.future.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
//@MapperScan(basePackages = {"com.future.service"})
public class AccessTokenController {

    //    @Resource
//    private AccessPrivider accessPrivider;
    @Autowired
    private UserService userService;

    @RequestMapping("/callback")
    public void callback(Model model, @RequestParam(name = "code")String code,
                         @RequestParam(name = "state") String state, HttpSession session, HttpServletResponse response) throws IOException {
        System.out.println(code);
        PrividerToken prividerToken = new PrividerToken();
        prividerToken.setClient_id("f0983305363b7ecf81d6bb12844114c4e250688d1e7811dcbe6bbece913b2f7e");
        prividerToken.setCode(code);
        prividerToken.setRedirect_uri("http://localhost:8080/future/callback");
        prividerToken.setState(state);
        prividerToken.setClient_Secret("b26d34b345f9126f5e3681704200bd0c2e71045a5c1a0eca10437c10932afc4d");
        AccessPrivider accessPrivider = new AccessPrivider();
        String Token = accessPrivider.getToken(prividerToken);
        System.out.println(Token);
        Users users = accessPrivider.getUser(Token);
        System.out.println(users.getId());
        Users user = userService.giteeLogin(users.getId());
        if(user!=null){
            session.setAttribute("user",user);
            response.sendRedirect("/future/");
        }else{
            session.setAttribute("Id",users.getId());
            response.sendRedirect("/future/BindCount");
        }

    }
}